package com.study;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author:李雄
 * @ClassName:StreamAPITest
 * @Description:Stream流的测试
 * @createTime:2022-08-21 21:47
 */
public class StreamAPITest {

    //方式一：通过集合
    @Test
    public void test1(){
        List<EmployeeData> list = new ArrayList<>();
        list.add(new EmployeeData(1001,"马化腾",34,6000.83));
        list.add(new EmployeeData(1002,"雷军",12,9800.00));
        list.add(new EmployeeData(1003,"刘强东",33,5800.12));
        list.add(new EmployeeData(1004,"比尔盖茨",26,10900.00));
        list.add(new EmployeeData(1005,"马云",65,1000000.00));
        list.add(new EmployeeData(1006,"李彦宏",35,4000.00));

        //default Stream<E> stream();顺序流
        Stream<EmployeeData> stream = list.stream();
        //default Stream<E> parallelStream();返回一个并行流
        Stream<EmployeeData> employeeDataStream = list.parallelStream();
    }

    //方式二：通过数组
    public void test2(){
        int[] arr =new int[] {1,2,3,4};
        //通过调用Arrays类的static <T> Stream<T> stream(T[] array):返回一个流
        IntStream stream = Arrays.stream(arr);

        EmployeeData e1 = new EmployeeData(1001,"马化腾",34,6000.83);
        EmployeeData e2 = new EmployeeData(1003, "刘强东", 33, 5800.12);
        EmployeeData[] arr1 = new EmployeeData[]{e1,e2};
        Stream<EmployeeData> stream1 = Arrays.stream(arr1);

    }

    //方式三：Stream的of()
    @Test
    public void test3(){
        Stream<Integer> stream = Stream.of(1, 2, 3, 4, 5, 6);
    }

    //方式四：创建无限流
    @Test
    public void test4(){
        //迭代
        //public static<T> Stream<T> iterate(final T seed, final UnaryOperator<T> f)
        //遍历前10个偶数
        Stream.iterate(0,t->t+2).limit(10).forEach(System.out::println);

        //生成
        //public static<T> Stream<T> generate(Supplier<T> s)
        Stream.generate(Math::random).limit(10).forEach(System.out::println);
    }

}
